package com.test.deencredit.dialog_deencredit;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;

import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.test.deencredit.R;
import com.test.deencredit.listener_deencredit.OnDatePickListener_deencredit;

public class DatePickerDialog_deencredit extends BottomSheetDialog {

    private final String title_deencredit;
    private final OnDatePickListener_deencredit onDatePickListener_deencredit;

    public DatePickerDialog_deencredit(Context context, String title, OnDatePickListener_deencredit onDatePickListener){
        super(context, R.style.Theme_DeenCredit_BottomDialogStyle);
        this.title_deencredit = title;
        this.onDatePickListener_deencredit = onDatePickListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_date_picker_deencredit);

        getWindow().setBackgroundDrawableResource(R.color.transparent);
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        init_deencredit();
    }

    private void init_deencredit(){

        TextView titleTv = findViewById(R.id.ddp_tv_title);
        Button doneBtn = findViewById(R.id.ddp_btn_done);

        titleTv.setText(title_deencredit);
        findViewById(R.id.ddp_ib_close).setOnClickListener(v -> dismiss());

        DatePicker datePicker = findViewById(R.id.ddp_datepicker);
        if (datePicker == null) return;
        datePicker.setCalendarViewShown(false);

        doneBtn.setOnClickListener(v -> {
            int month = datePicker.getMonth() + 1;
            int day = datePicker.getDayOfMonth();
            String monthStr = month<10 ? "0"+month:String.valueOf(month);
            String dayStr = day<10 ? "0"+day:String.valueOf(day);
            String date = datePicker.getYear()+"-"+monthStr+"-"+dayStr;
            if (onDatePickListener_deencredit != null) onDatePickListener_deencredit.onDatePick_deencredit(date);
            DatePickerDialog_deencredit.this.dismiss();
        });
    }

}
